package com.ethan.rabbitttlproducer;

import lombok.Data;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
@Data
public class MyBean {
    private final AmqpAdmin amqpAdmin;
    private final AmqpTemplate amqpTemplate;

    @Autowired
    public MyBean(AmqpAdmin amqpAdmin, AmqpTemplate amqpTemplate) {
        this.amqpAdmin = amqpAdmin;
        this.amqpTemplate = amqpTemplate;
    }
    //声明队列
    @Bean("queue")
    Queue queue(){
        return QueueBuilder.durable("ethan.queue_ttl").ttl(10*1000).build();
    }

    //声明路由注意路由的类型为direct
    @Bean("exchange")
    Exchange exchange(){
        return ExchangeBuilder.topicExchange("ethan.exchange_ttl").build();
    }
    //绑定队列到路由并设置routingkey为routing_inforouting_error
    @Bean
    Binding binding(@Qualifier("queue") Queue queue,
                    Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("ethan.ttl.#").noargs();
    }

}
